export function copyToClipboard (string) {
  let copyInput = document.createElement('INPUT')
  copyInput.type = 'text'
  copyInput.style.width = '1px'
  copyInput.style.height = '1px'
  copyInput.style.border = 0
  copyInput.style.outline = 0
  document.body.appendChild(copyInput)
  copyInput.value = string
  copyInput.select()
  let result = document.execCommand('Copy')
  document.body.removeChild(copyInput)
  return result
}

export function setTitle (string) {
  document.title = string + '--a website of a phper, all thing will be better when you try'
}

export function scrollTo (end = 0) {
  document.body.scrollTop = end
  document.documentElement.scrollTop = end
}

export function getScrollTop () {
  let scrollTop = 0
  if (document.documentElement && document.documentElement.scrollTop) {
    scrollTop = document.documentElement.scrollTop
  } else if (document.body) {
    scrollTop = document.body.scrollTop
  }
  return scrollTop
}

export function getScrollHeight () {
  return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
}

export function getClientHeight () {
  let clientHeight = 0
  if (document.body.clientHeight && document.documentElement.clientHeight) {
    clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
  } else {
    clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
  }
  return clientHeight
}

export const getScrollToBottom = (y = 100) => {
  const scrollTop = document.documentElement.scrollTop ||
    window.pageYOffset ||
    document.body.scrollTop
  if (document.documentElement.scrollHeight - y <=
    document.documentElement.clientHeight + scrollTop
  ) {
    return true
  }
  return false
};

export const getScrollToBottomed = (container = false, y = 200) => {
  /* 滚动距离 */
  const scrollTop = container ?
    container.scrollTop || container.pageYOffset :
    document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop

  /* 容器高度 */
  const containerHeight = container ?
    container.clientHeight :
    document.documentElement.clientHeight

  /* 滚动高度 */
  const scrollHeight = container ?
    container.scrollHeight :
    document.documentElement.scrollHeight

  /* 距离底部高度 */
  const scrollBottom = scrollHeight - scrollTop - containerHeight

  if (scrollBottom <= y) {
    return true
  }
  return false
}